from square import Square
from team import Team
from piece import PlayerPiece, BallPiece
from commonerror import BoardError

class Board:
    """ Represents the actual Mastergoal board """
    
    (GOAL, SPECIAL, LITTLE, BIG, CORNER, AREA, CENTRAL) = range(7)
    
    board_areas = [
        [ AREA, CORNER, AREA, AREA, AREA, AREA, AREA, CENTRAL, AREA, AREA, AREA, AREA, AREA, CORNER, AREA ],
        [ AREA, BIG, BIG ,BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, BIG, BIG, AREA ],
        [ AREA, LITTLE, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, LITTLE, AREA ],
        [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
        [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
        [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
        [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
        [ GOAL, SPECIAL, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, SPECIAL, GOAL ],
        [ AREA, LITTLE, LITTLE, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, LITTLE, LITTLE, AREA ],
        [ AREA, BIG, BIG, BIG, BIG, AREA, AREA, CENTRAL, AREA, AREA, BIG, BIG, BIG, BIG, AREA ],
        [ AREA, CORNER, AREA, AREA, AREA, AREA, AREA, CENTRAL, AREA, AREA, AREA, AREA, AREA, CORNER, AREA ]           
    ]
    
    def __init__(self, ballpiece):
        self.teams = {}
        self.player_with_ball = None
        self.ballpiece = ballpiece
        
    def __getitem__(self, key):
        return self.teams[key]
        
    def add_team(self, team):
        if len(self.teams) > 1:
            raise BoardError("only two teams per board are allowed")

        self.teams[team.orientation] = team 
        
    def remove_team(self, team):
        del self.teams[team.orientation]
        
    def get_keepers(self):
        keepers = []
        for team in self.teams:
            for player in team.players:
                if player.id == PlayerPiece.G:
                    keepers.append(player)
        return keepers
            
    
    def own_corner(self, orientation, col, row):
        return (orientation == Team.DOWN and Board.board_areas[col][row] == Board.CORNER and row == 1) or (orientation == Team.UP and Board.board_areas[col][row] == Board.CORNER and row == 13)
    
    def big_area(self, orientation, col, row):
        return (orientation == Team.DOWN and Board.board_areas[col][row] <= Board.BIG and row < 7) or (orientation == Team.UP and Board.board_areas[col][row] <= Board.BIG and row > 7)
    
    def little_area(self, col, row):
        return ((orientation == Team.DOWN and Board.board_areas[col][row] <= Board.LITTLE and row < 7)
            or (orientation == Team.UP and Board.board_areas[col][row] <= Board.LITTLE and row > 7))
    
    def team_area(self, orientation, col, row):
         return (orientation == Team.DOWN and Board.board_areas[col][row] <= Board.AREA and row < 7) or (orientation == Team.UP and Board.board_areas[col][row] <= Board.AREA and row > 7)
     

    def is_goal(self, orientation, col, row):
        return (
            (orientation == Team.DOWN and Board.board_areas[col][row] == Board.GOAL)
            or (orientation == Team.UP and Board.board_areas[col][row] == Board.GOAL)
            )
        
    def special_square(self, orientation, col, row):
        return (
                (orientation == Team.DOWN and (Board.board_areas[col][row] == Board.CORNER or 
                                             Board.board_areas[col][row]== Board.SPECIAL) and row == 13) or 
                (orientation == Team.UP and (Board.board_areas[col][row] == Board.CORNER or 
                                               Board.board_areas[col][row] == Board.SPECIAL) and row == 1)
            )
    
    def opponent_in_square(self, orientation, col, row):
        
        opponent = orientation and Team.UP or Team.DOWN
        for player in self.teams[opponent].players:
            if player.square == Square(col, row):
                return True
            
        return False
        
        
        
        
    